package com.nvelickovic10.snake.beans.game;

import java.awt.Point;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileFilter;

import com.nvelickovic10.snake.beans.snake.Snake;
import com.nvelickovic10.snake.beans.snake.SnakeBits;
import com.nvelickovic10.snake.main.AppCore;

public class GameSave implements Serializable {

	private static final long serialVersionUID = 5005243624330799123L;
	private long speedFactor;
	private ArrayList<SnakeBits> body;
	private int points;
	private Point foodPosition;
	private String username;

	public GameSave() {

	}

	public void readData(GameSave old) {
		setBody(old.getBody());
		setFoodPosition(old.getFoodPosition());
		setPoints(old.getPoints());
		setSpeedFactor(old.getSpeedFactor());
		setUsername(old.getUsername());
	}

	public void setData() {
		Snake.getInstance().setBody(body);
		AppCore.getInstance().setFoodPosition(foodPosition);
		AppCore.getInstance().setPoints(points);
		AppCore.getInstance().setSpeedFaktor(speedFactor);
		AppCore.getInstance().setUsername(username);
		AppCore.getInstance().repaint();
	}

	public void save() {
		JFileChooser jfc = new JFileChooser();
		jfc.setCurrentDirectory(new File("saves/"));
		jfc.setFileFilter(new FileFilter() {

			@Override
			public String getDescription() {
				return null;
			}

			@Override
			public boolean accept(File f) {
				if (f.isDirectory() || f.getName().endsWith(".ssf"))
					return true;
				return false;
			}
		});
		jfc.showSaveDialog(null);
		if (jfc.getSelectedFile() != null) {
			File file;
			if (jfc.getSelectedFile().getAbsolutePath().toString()
					.endsWith(".ssf")) {
				file = jfc.getSelectedFile();
				FileInputStream fis;
				try {
					fis = new FileInputStream(file);
					ObjectInputStream ois = new ObjectInputStream(fis);
					GameSave in = (GameSave) ois.readObject();
					ois.close();
					fis.close();
					readData(in);
					AppCore.getInstance().setPaused(true);
					if (!AppCore.getInstance().getUsername()
							.equals(this.username)) {
						JOptionPane
								.showMessageDialog(null,
										"THIS IS NOT YOUR SAVED GAME!!!\nGAME NOT SAVED!");
						return;
					}
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			} else {
				file = new File(jfc.getSelectedFile().toString() + ".ssf");
			}
			try {
				FileOutputStream fout = new FileOutputStream(
						file.getAbsoluteFile());
				ObjectOutputStream oos = new ObjectOutputStream(fout);
				oos.writeObject(this);
				oos.close();
				fout.close();
				JOptionPane.showMessageDialog(null, "GAME SAVED!");
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	public void load() {
		JFileChooser jfc = new JFileChooser();
		jfc.setCurrentDirectory(new File("saves/"));
		jfc.setFileFilter(new FileFilter() {

			@Override
			public String getDescription() {
				// TODO Auto-generated method stub
				return null;
			}

			@Override
			public boolean accept(File arg0) {
				if (arg0.isDirectory()
						|| arg0.getAbsolutePath().endsWith(".ssf"))
					return true;
				return false;
			}
		});
		jfc.showOpenDialog(null);
		if (jfc.getSelectedFile() != null) {
			File file = jfc.getSelectedFile();
			try {
				FileInputStream fis = new FileInputStream(file);
				ObjectInputStream ois = new ObjectInputStream(fis);
				GameSave in = (GameSave) ois.readObject();
				ois.close();
				fis.close();
				readData(in);
				AppCore.getInstance().setPaused(true);
				if (!AppCore.getInstance().getUsername().equals(this.username)) {
					JOptionPane.showMessageDialog(null,
							"THIS IS NOT YOUR SAVED GAME!!!\nGAME NOT LOADED!");
				} else {
					setData();
					JOptionPane.showMessageDialog(null, "GAME LOADED!!!");
				}
				AppCore.getInstance().setPaused(false);
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	public long getSpeedFactor() {
		return speedFactor;
	}

	public void setSpeedFactor(long speedFactor) {
		this.speedFactor = speedFactor;
	}

	public ArrayList<SnakeBits> getBody() {
		return body;
	}

	public void setBody(ArrayList<SnakeBits> arrayList) {
		this.body = arrayList;
	}

	public int getPoints() {
		return points;
	}

	public void setPoints(int points) {
		this.points = points;
	}

	public Point getFoodPosition() {
		return foodPosition;
	}

	public void setFoodPosition(Point foodPosition) {
		this.foodPosition = foodPosition;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

}
